// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
	"strings"
)

func helloWorld(w http.ResponseWriter, r *http.Request) {
	// r.ParseForm()
	fmt.Fprintf(w, "Hello World!")
}

//	例子：用于说明GET和POST的区别
//	第一次用于请求同意
func index(w http.ResponseWriter, r *http.Request) {
	t, _ := template.ParseFiles("index-01-getAndPost.html")
	t.Execute(w, nil)
}

//	第二次，用于判断登录用户名密码是否正确
func login(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()

	for k, v := range r.Form {
		fmt.Println("	key:", k)
		fmt.Println("	value:", strings.Join(v, ""))
	}

	if r.Form["username"][0] == "whf" && r.Form["password"][0] == "whf15" {
		fmt.Fprintf(w, "登陆成功！")
	} else {
		fmt.Fprintf(w, "未知名错误")
	}
}
func main() {
	// fmt.Println("Hello, 世界")
	http.HandleFunc("/helloWorld", helloWorld)

	//用于说明GET和POST的区别
	http.HandleFunc("/index", index)
	http.HandleFunc("/login", login)

	err := http.ListenAndServe(":9092", nil) // 服务器端的监听端口
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
